package com.stars.home.service;

import android.text.TextUtils;
import android.util.Log;
import com.tencent.mmkv.MMKV;
import com.trailer.base.utils.S;
import com.zhangke.websocket.SimpleListener;
import com.zhangke.websocket.SocketListener;
import com.zhangke.websocket.WebSocketHandler;
import com.zhangke.websocket.response.ErrorResponse;

import org.json.JSONException;
import org.json.JSONObject;

import java.nio.ByteBuffer;


public class MsgService {


    public static MsgService msgService;

    public static MsgService getInstance() {
        if (msgService == null) {
            msgService = new MsgService();
        }
        return msgService;
    }


    public void connect() {

        WebSocketHandler.getDefault().addListener(socketListener);

    }

    public boolean send(String data) {//发送消息
        if (WebSocketHandler.getDefault() != null) {

            WebSocketHandler.getDefault().send(data);
            return true;
        }

        return false;

    }


    public void reconnect() {
        WebSocketHandler.getDefault().start();//重新连接

    }


    public void closeSocket() {//关闭
        // WebSocketHandler.getDefault().removeListener(socketListener);
        WebSocketHandler.getDefault().disConnect();//断开连接
    }


    private SocketListener socketListener = new SimpleListener() {
        @Override
        public void onConnected() {
            //连接成功

            appendMsgDisplay("onConnected");

            String uid = MMKV.defaultMMKV().getString("userId", "");
            if (!TextUtils.isEmpty(uid)) {
                SocketUtils.initRoom(uid);

            }


        }

        @Override
        public void onConnectFailed(Throwable e) {
            if (e != null) {
                appendMsgDisplay("onConnectFailed:" + e.toString());
            } else {
                appendMsgDisplay("onConnectFailed:null");
            }
        }

        @Override
        public void onDisconnect() {//连接失败
            //"链接断开，正在尝试重连..."
            appendMsgDisplay("onDisconnect");
        }

        @Override
        public void onSendDataError(ErrorResponse errorResponse) {
            appendMsgDisplay("onSendDataError:" + errorResponse.toString());
            errorResponse.release();


        }

        @Override
        public <T> void onMessage(String message, T data) {


            appendMsgDisplay(message);
        }

        @Override
        public <T> void onMessage(ByteBuffer bytes, T data) {
            //appendMsgDisplay("onMessage(ByteBuffer, T):" + bytes);
        }
    };


    private void appendMsgDisplay(String msg) {


        try {
            JSONObject jsonObject = new JSONObject(msg);
            String message_type = jsonObject.optString("cmd");
            if ("Group_init_S".equals(message_type)) {
                JSONObject to = jsonObject.getJSONObject("data");
                String client_id = to.getString("client_id");

                new S().putString("client_id", client_id);


            } else if ("Group_chat_S".equals(message_type)) {//普通消息和 系统消息 任务消息

                Log.e("-88--", "msg:" + msg);


                String data = jsonObject.optString("data");
                JSONObject v = new JSONObject(data);
                String to = v.optString("to");


            }

        } catch (JSONException e) {
//            e.printStackTrace();
        }


    }


}
